/**
 * Created by Sxd on 2014/9/28.
 */

var UiUtil = UiUtil || {};

function makeDWord (low, high) {
    return (high << 16) | (low & 0xFFFF);
}

function parseDWord (dword) {
    var high = dword >> 16;
    var low = dword & 0x0000FFFF;
    return [low, high];
}

/**
 * 禁用/开启一个 widget
 * @param {ccui.Widget} widget
 * @param {boolean} enable
 */
UiUtil.ccuiEnable = function (widget, enable) {
    widget.setBright(enable);
    widget.enabled = enable;
};

/**
 * 播放一个动画
 * @param {ccs.Armature} armature 动画对象
 * @param {Function} [onCompleted] 播放完成回调
 * @param {*} [context] onCompleted的this
 * @param [loop=0] 是否循环 默认不循环
 * @param [durationTo=-1]
 */
UiUtil.playAnimation = function (armature, onCompleted, context, loop, durationTo) {
    if (!armature) return;
    loop = loop || 0;
    durationTo = durationTo || -1;
    var ani = armature.getAnimation();
    ani.playWithIndex(0, durationTo, loop);
    if (onCompleted) {
        context = context || this;
        ani.setMovementEventCallFunc(function (armature, movementType, movementID) {
            if ((movementType == ccs.MovementEventType.complete) || (movementType == ccs.MovementEventType.loopComplete)) {
                onCompleted.call(context);
            }
        }, context);
    }
    armature.visible = true;
};

/**
 * 顺序播放动画
 * @param {ccs.Armature[]} armatures 要播放的动画
 * @param {Function[]} [onOneCompleted] 每一个动画播放完成的回调
 * @param {Function} [onCompleted] 播放完成回调
 * @param {*} [context] onCompleted的this
 */
UiUtil.playAnimationsSeries = function (armatures, onOneCompleted, onCompleted, context) {
    var calls = [];
    context = context || this;

    cc.each(armatures, function (armature, index) {
        var fun = function (cb) {
            var self = this;
            UiUtil.playAnimation(armature, function () {
                if (onOneCompleted[self])
                {
                    onOneCompleted[self].call(context);
                }
                cb();
            });
        };
        calls[index] = fun.bind(index);
    });

    cc.async.series(calls, function (err, res) {
        if (onCompleted) {
            onCompleted.call(context);
        }
    });
};

UiUtil.isShaking = false;
/**
 * 震动
 * @param dis
 * @param [times=2]
 * @param [offset=5]
 * @param [speed=0.032]
 */
UiUtil.shake = function (dis, times, offset, speed) {
    times = times || 2;
    offset = offset || 5;
    speed = speed || 0.032;
    if (this.isShaking) {
        return;
    }
    this.isShaking = true;
    var point = cc.p(dis.x, dis.y);
    var offsetXYArray = [0, 0];
    var num = 0;
    var u = function () {
        offsetXYArray[num % 2] = (num++) % 4 < 2 ? 0 : offset;
        if (num > (times * 4 + 1)) {
            num = 0;
            UiUtil.isShaking = false;
            dis.x = point.x;
            dis.y = point.y;
            return;
        }
        dis.x = offsetXYArray[0] + point.x;
        dis.y = offsetXYArray[1] + point.y;
        xinyou.timer.setTimeout(u, speed);
    };
    u();
};

var MathUtil = MathUtil || {};

/**
 * 随机数
 * @param {number} min
 * @param {number} max
 */
MathUtil.random = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
};